layui.define(['form','table','element'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table,element = layui.element;

  var deviceSn = $('input[name="deviceSn"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/cdcz/device/slot/list.data'
      ,where:{deviceSn:deviceSn}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'slotNum', width:80, title: '卡槽号'}
      ,{field: 'overloadStateName', width:80,title: '过载'}
      ,{field: 'temperatureStateName', width:80,title: '温度'}
      ,{field: 'stateName', width:80,title: '状态'}
      ,{field: 'currentTradeNo', title: '最近订单'}
      ,{field: 'updateDate', title: '最后修改时间'}
      ,{title: '操作', width: 160, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });

    table.render({
        elem: '#LAY-table-slave-device'
        ,url: layui.setter.webRoot + '/cdcz/device/slave_device/list.data'
        ,where:{deviceSn:deviceSn}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'bindIndex',title: '绑定序号'}
            ,{field: 'deviceSn',title: '从机设备号'}
            ,{field: 'slotTotal',title: '卡槽数'}
            ,{field: 'stateName',title: '状态'}
            ,{field: 'bindDate', title: '绑定时间'}
            ,{field: 'createDate', title: '创建时间'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    element.on('tab(tab-info)', function (data) {
        if(data.index == 0) {
            table.reload('LAY-table')
        }else if(data.index== 1){
            table.reload('LAY-table-slave-device')
        }
    });

  //事件
  var active = {
    search:function(){
        layer.confirm('确定要发送查询命令吗？', function(index){
            $.ajax({
                type: "post",
                url: layui.setter.webRoot + '/cdcz/device/operate/search.do',
                data:{deviceSn:deviceSn},
                dataType:"json",
                success:function(res){
                    if(res.code == 200){
                        layer.msg('查询成功',{icon: 1},function (){
                            table.reload('LAY-table'); //数据刷新
                        },5000);
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                }
            });
            layer.close(index);
        });
    }
    ,restart:function(){
        layer.confirm('确定要重启设备吗？', function(index){
          $.ajax({
              type: "post",
              url: layui.setter.webRoot + '/cdcz/device/operate/restart.do',
              data:{deviceSn:deviceSn},
              dataType:"json",
              success:function(res){
                  if(res.code == 200){
                      layer.msg('设备重启成功',{icon: 1},function (){
                          table.reload('LAY-table'); //数据刷新
                      },5000);
                  }else{
                      layer.msg(res.msg,{icon: 5});
                  }
              }
          });
          layer.close(index);
        });
    }
    ,changeMQTT:function (){
          layer.open({
              type: 2
              ,title: '切换MQTT连接'
              ,content: layui.setter.webRoot + '/cdcz/device/operate/mqtt_connect/change.html?deviceSn='+deviceSn
              ,maxmin: true
              ,area: ['500px', '550px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/device/operate/mqtt_connect/change.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table'); //数据刷新
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,charge: function(obj){
      layer.open({
        type: 2
        ,title: '测试充电'
        ,content: layui.setter.webRoot + '/cdcz/device/operate/slot/charge.html?deviceSn='+obj.data.deviceSn + '&slotNum='+obj.data.slotNum
        ,maxmin: true
        ,area: ['500px', '450px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field;
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/cdcz/device/operate/slot/charge.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
						table.reload('LAY-table'); //数据刷新
						layer.close(index); //关闭弹层
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,closeCharge:function(obj){
    	layer.confirm('确定要发送关闭充电命令吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/cdcz/device/operate/slot/charge/close.do',
	          data:{deviceSn:obj.data.deviceSn,slotNum:obj.data.slotNum},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
                    table.reload('LAY-table'); //数据刷新
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
    ,bindSlaveDevice: function(){
          layer.open({
              type: 2
              ,title: '绑定从机'
              ,content: layui.setter.webRoot + '/cdcz/device/operate/slave_device/bind.html?mainDeviceSn='+deviceSn
              ,maxmin: true
              ,area: ['500px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/device/operate/slave_device/bind.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table-slave-device'); //数据刷新
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,findSlaveDevice: function(){
          layer.open({
              type: 2
              ,title: '查询从机'
              ,content: layui.setter.webRoot + '/cdcz/device/operate/slave_device/find.html?mainDeviceSn='+deviceSn
              ,maxmin: true
              ,area: ['400px', '250px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/device/operate/slave_device/find.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table-slave-device'); //数据刷新
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,delSlaveDevice:function(obj){
          layer.confirm('确定要移除所有从机吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/cdcz/device/operate/slave_device/remove.do',
                  data:{mainDeviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          table.reload('LAY-table-slave-device'); //数据刷新
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('charge_socket_v2_info', {})
});